package io.mft.api.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import cn.hutool.core.util.ObjectUtil;
import io.mft.commons.function.response.DsName;
import io.mft.commons.util.Unique;

@JsonInclude(Include.NON_DEFAULT)
public class Output implements Unique,Serializable{
	 
	        /**
	 * @说明：TODO
	 * @字段名称 serialVersionUID
	        */  
	    
	private static final long serialVersionUID = -2284158162321625778L;
	@JsonIgnore
	private List<OutputParameter> outputParameters;
	@JsonIgnore
	private boolean page=false;
	private String mainOutputDsKey;
	private List<OutputDs> outputDsList;
	@JsonInclude(Include.NON_EMPTY) 
	private String reqTagKey="";
	@JsonIgnore
	private Map<String, OutputDs> map=new HashMap<>();
	private boolean toCamelCase=false;
	@JsonInclude(Include.NON_NULL) 
	private OutputCache cache;
	private boolean responseSimpleData=false;
	public void setPage(boolean page) {
		this.page = page;
	}
	public boolean isPage() {
		return page;
	}
	public Map<String, OutputParameter> outputParameterMap(String key) {
		Map<String, OutputParameter> tmap = new HashMap<>();
		List<OutputParameter> list=this.map.get(key).getOutputParameters();
		list.forEach(o -> {
			tmap.put(o.getKey(), o);
		});
		return tmap;
	}
	public void setResponseSimpleData(boolean responseSimpleData) {
		this.responseSimpleData = responseSimpleData;
	}
	
	public boolean isResponseSimpleData() {
		return responseSimpleData;
	}
	public Map<String, OutputDs> outputDsMap() {
		return map;
	}
	@Override
	public String uniqueId() {
		String k="";
		for (OutputDs outputDs : outputDsList) {
			for (OutputParameter outputParameter : outputDs.getOutputParameters()) {
				k=k+"-"+outputParameter.uniqueId();
			}
			k=k+"-"+toCamelCase;
		}
		k=k+"-"+cache.uniqueId();
		return k;
	}
	
	public String getMainOutputDsKey() {
		return mainOutputDsKey;
	}
	public void setMainOutputDsKey(String mainOutputDsKey) {
		this.mainOutputDsKey = mainOutputDsKey;
	}
	public void setOutputDsList(List<OutputDs> outputDsList) {
		this.outputDsList = outputDsList;
	}
	public List<OutputDs> getOutputDsList() {
		return outputDsList;
	}
	public void setToCamelCase(boolean toCamelCase) {
		this.toCamelCase = toCamelCase;
	}
	public boolean isToCamelCase() {
		return toCamelCase;
	}
	
	public void setOutputParameters(List<OutputParameter> outputParameters) {
		this.outputParameters = outputParameters;
	}
	public List<OutputParameter> getOutputParameters() {
		return outputParameters;
	}
	public String getReqTagKey() {
		return reqTagKey;
	}
	public void setReqTagKey(String reqTagKey) {
		this.reqTagKey = reqTagKey;
	}
	public void initDefaultDs() {
		if (outputDsList==null) {
			outputDsList=new ArrayList<OutputDs>();
		}
		if (outputParameters!=null) {
			OutputDs outputDs=new OutputDs();
			outputDs.setDsKey(DsName.DEFAULT_RESULT_KEY);
			outputDs.setNote("默认结果集");
			outputDs.setOutputParameters(outputParameters);
			outputDs.setPage(isPage());
			outputDsList.add(outputDs);
		}
		for (OutputDs outputDs : outputDsList) {
			map.put(outputDs.getDsKey(), outputDs);
		}
		if (ObjectUtil.isEmpty(mainOutputDsKey)) {
			if (outputDsList.size()>0) {
				mainOutputDsKey=outputDsList.get(0).getDsKey();
			}else {
				mainOutputDsKey=DsName.DEFAULT_RESULT_KEY;
			}
		}
		if (outputParameters==null) {
			OutputDs mainDs=map.get(mainOutputDsKey);
			if (mainDs!=null) {
				outputParameters=mainDs.getOutputParameters();
			}
		}
		if (cache==null) {
			cache=new OutputCache();
		}
	}
	
	public OutputCache getCache() {
		return cache;
	}
	public void setCache(OutputCache cache) {
		this.cache = cache;
	}
}
